﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DevExpress.Web.Mvc;
using KimPhongCRM.Models;

namespace KimPhongCRM.Controllers
{
    public class NotifyController : BaseController
    {
        private Entities db = new Entities();
        List<KimPhongCRM.Models.sp_Select_Combobox_User_Result> listUser = new List<sp_Select_Combobox_User_Result>();
        public NotifyController()
        {
            listUser = db.sp_Select_Combobox_User().ToList();
        }
        //
        // GET: /Notify/
        [MvcSiteMapProvider.MvcSiteMapNode(Title = "Ghi chú", Key = "Notify", Clickable = true, ParentKey = "Home")]
        public ActionResult Index()
        {
            return View();
        }
        [MvcSiteMapProvider.MvcSiteMapNode(Title = "Danh sách thao tác", Key = "TacVu", Clickable = true, ParentKey = "Notify")]
        public ActionResult GetTop10(string user_id)
        {
            var model = db.sp_Select_Notify(user_id.ToString()).ToList();
            return PartialView("~/Views/Shared/NotifyPartial.cshtml", model);
        }
        [MvcSiteMapProvider.MvcSiteMapNode(Title = "Sinh nhật khách hàng", Key = "snKhachHang", Clickable = true, ParentKey = "KhachHang")]
        public ActionResult GetBirthday7()
        {
            var model = db.sp_Select_KhachHang_Birthday(7).ToList();
            return PartialView("~/Views/Shared/BirthdayPartial.cshtml", model);
        }
        [MvcSiteMapProvider.MvcSiteMapNode(Title = "Tin Nhắn", Key = "TinNhan", Clickable = true, ParentKey = "Notify")]
        public ActionResult GetMessageTop10()
        {
            //var model = db.tblMessages;
            return PartialView("~/Views/Shared/MessagePartial.cshtml");//, model);
        }

        public JsonResult UpdateNotify(string user_id)
        {
            var model = db.sp_Select_Notify(user_id.ToString()).ToList();
            return Json(model,JsonRequestBehavior.AllowGet);
        }

        public ActionResult PopupDetail(long malichsu, string tablename)
        {
            System.Data.Objects.ObjectResult<sp_Select_Detail_Notify_Result> nt = db.sp_Select_Detail_Notify(malichsu, tablename);
            sp_Select_Detail_Notify_Result model = nt.ToList<sp_Select_Detail_Notify_Result>()[0];
            /*Get userid*/
            var uid = KimPhongCRM.Lib.SessionData.GetSession("sdUserId").ToString();
            Guid? user_id;
            if (uid != "")
            {
                user_id = new Guid(KimPhongCRM.Lib.SessionData.GetSession("sdUserId").ToString());
            }
            else
            {
                KimPhongCRM.Controllers.BaseController bcontrol = new KimPhongCRM.Controllers.BaseController();
                string un = User.Identity.Name;
                bcontrol.ressUserID(un);
                user_id = new Guid(KimPhongCRM.Lib.SessionData.GetSession("sdUserId").ToString());
            }

            Dictionary<string, object> dic = new Dictionary<string,object>();
            string viewer ="";
            if(model.reader!=null&&model.reader!="")
            {
                dic = Lib.convertParameters.parameter(model.reader.ToString());
                if (!model.reader.Contains(user_id.ToString()))
                {
                    int number = dic.Count();
                    dic.Add("reader" + number.ToString(), user_id);
                }
                viewer = Lib.convertParameters.strparameter(dic);
           }
            else
            {
                dic.Add("reader",user_id);
                viewer = Lib.convertParameters.strparameter(dic);
            }
            db.sp_Change_Notify_Viewer(malichsu, tablename, viewer);
             /*Get userid*/
            return View(model);
        }

        [ValidateInput(false)]
        public ActionResult ListNotifyPartial()
        {
            ViewData["listUser"] = listUser;
            var model = db.sp_Select_Notify_All("").ToList();
            return PartialView("_ListNotifyPartial", model);
        }
        [HttpPost, ValidateInput(false)]
        public ActionResult ListNotifyPartialAddNew(KimPhongCRM.Models.sp_Select_Notify_Result item)
        {
            var model = db.sp_Select_Notify_All("").ToList();
            if (ModelState.IsValid)
            {
                try
                {
                    // Insert here a code to insert the new item in your model
                }
                catch (Exception e)
                {
                    ViewData["EditError"] = e.Message;
                }
            }
            else
                ViewData["EditError"] = "Please, correct all errors.";
            return PartialView("_ListNotifyPartial", model);
        }
        [HttpPost, ValidateInput(false)]
        public ActionResult ListNotifyPartialUpdate(KimPhongCRM.Models.sp_Select_Notify_Result item)
        {
            var model = new object[0];
            if (ModelState.IsValid)
            {
                try
                {
                    // Insert here a code to update the item in your model
                }
                catch (Exception e)
                {
                    ViewData["EditError"] = e.Message;
                }
            }
            else
                ViewData["EditError"] = "Please, correct all errors.";
            return PartialView("_ListNotifyPartial", model);
        }
        [HttpPost, ValidateInput(false)]
        public ActionResult ListNotifyPartialDelete(System.Int64 maLichSu)
        {
            var model = new object[0];
            if (maLichSu != null)
            {
                try
                {
                    // Insert here a code to delete the item from your model
                }
                catch (Exception e)
                {
                    ViewData["EditError"] = e.Message;
                }
            }
            return PartialView("_ListNotifyPartial", model);
        }
    }
}
